﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using ProjectBase.Core;

using Survey.Common;
using Survey.Common.Validation;

namespace Survey.Business
{
    public class FilterValidatorBase<TFilter, TID> : ValidatorBase<TFilter>, IFilterValidator<TFilter, TID>
        where TFilter : class, IFilter<TID>
    {
        public bool Validate(TFilter item, IPublishingReporter reporter)
        {
            return Validate(item, ValidateOn.Find, reporter);
        }
        protected override void Init(RuleCollection<TFilter> rules)
        {
            Rules.For(filter => filter.MaxRowsPerPage).RequireValueBetween(1, 999)
                .WhenFiltering()
                .ApplyOnClient();

            Rules.Assure(CheckMaxRowsPageLimit)
                .WhenFiltering()
                .ApplyOnServer();
        }

        protected const int MaxRows = 999;
        protected ValidationResult CheckMaxRowsPageLimit(TFilter filter, out ValidationMessage message)
        {
            message = null;
            var isRowsLimitCorrect = filter.MaxRowsPerPage <= MaxRows
                || filter.ExportAllRows;

            // OK
            if (isRowsLimitCorrect)
            {
                return ValidationResult.IsValid;
            }

            // too much rows
            message = new ValidationMessage(Str.Messages.ValueTooHigh4)
            {
                "MaxRowsPerPage".GetLocalized(),
                "Filter",
                filter.MaxRowsPerPage ,
                MaxRows,
            };
            return ValidationResult.Error;
        }

    }
}